<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            position: relative;
            margin-top: 50px;
            margin-left: 50px;
            width: 200px;
            height: 200px;
            background-color: lightgreen;
            opacity: 1;
        }
    </style>
</head>
<body>
    <div class="box"></div>

    <script>
        var box = document.querySelector('.box');

        box.onclick = function () {
            animation(this, {
                width: 300,
                height: 500,
                opacity: .3,
                zIndex: 66
            })
        }

        function animation(el, styleObj) {
            // 清除定时器
            if (el.timer) {
                clearInterval(el.timer)
            }
            el.timer = setInterval(function () {
                var stopFlag = true;
                // 循环样式对象
                for (key in styleObj) {
                    var nowValue = parseInt(window.getComputedStyle(el, 'null')[key]);
                    // 目标属性与实际属性相等，跳过
                    if (nowValue === styleObj[key]) {
                        continue
                    }
                    // z-index判断
                    if (key === 'zIndex') {
                        el.style[key] = styleObj[key];
                        continue
                    }
                    // opacity判断
                    if (key === 'opacity') {
                        nowValue = parseInt(window.getComputedStyle(el, 'null')[key] * 100);
                        var target = styleObj[key] * 100;
                    } else {
                        var target = styleObj[key]
                    }
                    var step = (target - nowValue) / 10;
                    // 修正步长
                    if (Math.abs(step) < 1) {
                        step = step > 0 ? 1 : Math.floor(step)
                    }
                    // 赋值
                    if (key === 'opacity') {
                        el.style[key] = (nowValue + step) / 100;
                    } else {
                        el.style[key] = nowValue + step + 'px';
                    }
                    // 只要有样式没有改变完成stopFlag = false
                    if (nowValue + step !== target) {
                        stopFlag = false
                    }
                }
                // 停止条件
                if (stopFlag) {
                    clearInterval(el.timer);
                    el.timer = null;
                    console.log('stop...');
                }
            }, 1000/60)
        }
    </script>
</body>
</html>